home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 23 / sysact11.zip / SYSACT.ASM < prev    next >
Assembly Source File  |  1987-02-02  |  13KB  |  654 lines

  1.  
  2. page 60,132
  3. title System Activity Monitor v1.1 2/2/87
  4.  
  5. ;
  6. ; SysAct - System Activity Monitor for DOS
  7. ;
  8. ; S.H.Smith, 31-jan-87
  9. ;
  10. ; This system activity facility is provided free of charge.  You may
  11. ; copy and distribute this package for any non-commercial purpose.
  12. ; Please contact the author for licensing for other applications.
  13. ;
  14. ; This document and the associated programs are copyright material
  15. ; and should not be modified or sold.
  16. ;
  17. ; Copyright (C) 1987 Samuel H. Smith,
  18. ; All commercial rights reserved.
  19. ;
  20. ; Address all inquiries to:
  21. ;        S.H.Smith
  22. ;        5119 N. 11th Ave 332
  23. ;        Phoenix, Az 85013
  24. ;
  25.  
  26. cseg segment 'code'
  27.    assume cs:cseg,ds:nothing,es:nothing,ss:nothing
  28.    org 100h
  29.  
  30. entry:
  31.    jmp startup
  32.  
  33.  
  34. page
  35. subttl Working Storage Section
  36. ;=========================================
  37.  
  38. ;pointers to original interrupt handlers
  39. old_int_20      dd 0
  40. old_int_21      dd 0
  41.  
  42. ;the activity log filename
  43. log_filename    db 'C:\SYSACT.LOG',0
  44.  
  45. ;the program that was executed
  46. exec_name       db 0
  47.                 db 13,10,'SysAct (C) 1987 Samuel H. Smith, '
  48.                 db 'all commercial rights reserved.',13,10
  49.  
  50. ;the time when the program was started
  51. exec_hour       db 0
  52. exec_min        db 0
  53. exec_sec        db 0
  54. exec_hund       db 0
  55.  
  56. ;the time when it terminated
  57. stop_hour       db 0
  58. stop_min        db 0
  59. stop_sec        db 0
  60. stop_hund       db 0
  61.  
  62. ;some resource usage statistics
  63. exec_reads      dw 0
  64. exec_writes     dw 0
  65. exec_others     dw 0
  66.  
  67.  
  68. ;-----------------------------------------
  69. ; record layout for the log file
  70. ;
  71.  
  72. log_record = log_ident      ;start of the logging record
  73.  
  74. log_length = (offset signature-offset log_record)    
  75.                             ;total bytecount of the log entry
  76.  
  77. ;filename of the program
  78.    log_ident      db 'filename.ext '
  79.  
  80. ;date of execution
  81.    log_year       db 'yy-'             ;year - 80=1980
  82.    log_month      db 'mm-'             ;month - 01=jan
  83.    log_day        db 'dd '             ;day of month
  84.  
  85. ;time of execution
  86.    log_hour       db 'hh:'
  87.    log_min        db 'mm:'
  88.    log_sec        db 'ss '
  89.  
  90. ;runtime
  91.    log_runhour    db 'hh:'
  92.    log_runmin     db 'mm:'
  93.    log_runsec     db 'ss.'
  94.    log_runhund    db 'hh '
  95.  
  96. ;resource usage counts
  97.    log_reads      db 'rrrr '
  98.    log_writes     db 'wwww '
  99.    log_others     db 'oooo'
  100.  
  101.                   db 13,10
  102.  
  103. ;installation signature - this pattern is checked to decide if sysact
  104. ;is already resident.
  105. signature         dw 'Sa'
  106.  
  107. ;eof marker to stop garbage (in case somebody types the .com file)
  108.                   db 26
  109.  
  110.  
  111. page
  112. subttl Subroutine Section
  113. ;=========================================
  114.  
  115. ;----------------------------------------
  116. ; push all registers macro
  117. ;
  118. pushall macro
  119.    push dx
  120.    push cx
  121.    push bx
  122.    push ax
  123.    push bp
  124.    push si
  125.    push di
  126.    push es
  127.    push ds
  128.    endm
  129.  
  130.  
  131. ;----------------------------------------
  132. ; pop all registers macro
  133. ;
  134. popall macro
  135.    pop ds
  136.    pop es
  137.    pop di
  138.    pop si
  139.    pop bp
  140.    pop ax
  141.    pop bx
  142.    pop cx
  143.    pop dx
  144.    endm
  145.  
  146.  
  147. page
  148. ;-----------------------------------------
  149. ; convert word to hex
  150. ;
  151. ; usage:  wtoh destination,source
  152. ;
  153.    assume ds:cseg
  154.  
  155. wtoh macro destination,source
  156.    ifdif <cx>,<source>
  157.       mov cx,source
  158.    endif
  159.    mov di,offset destination
  160.    call wtohs                  ;conversion subroutine
  161.    endm
  162.  
  163. ;
  164. ;convert byte in ah to hex digits
  165. ;
  166. wtohs proc near
  167.    call wtoh2       ;do high byte
  168.    mov ch,cl        ;fall through to low byte
  169.  
  170. wtoh2:
  171.    mov al,ch
  172.    ror al,1
  173.    ror al,1
  174.    ror al,1
  175.    ror al,1         ;do high nibble
  176.    call wtoh1
  177.    mov al,ch        ;fall through to low nibble
  178.  
  179. wtoh1:
  180.    and al,15        ;mask unused bits
  181.    add al,'0'       ;convert to decimal
  182.    cmp al,'9'       ;check for a..f
  183.    jbe wtoh0
  184.  
  185.    add al,39        ;adjust for a..f
  186. wtoh0:
  187.    mov cs:[di],al   ;place the digit
  188.    inc di
  189.    ret
  190.  
  191.    assume ds:nothing
  192. wtohs endp
  193.  
  194.  
  195. page
  196. ;-----------------------------------------
  197. ; convert integer to digits
  198. ;
  199. ; format:  itod destination,source
  200. ; exit:    ax changed
  201. ;
  202.    assume ds:cseg
  203.  
  204. itod macro destination,source
  205.    ifdif <al>,<source>
  206.       mov al,source
  207.    endif
  208.    mov di,offset destination
  209.    call itods                  ;convert integer to digits subroutine
  210.    endm
  211.  
  212. ;
  213. ;integer to 2 digit ascii conversion
  214. ;
  215. itods proc near
  216.    push cx
  217.    mov ah,0
  218.    mov cl,10
  219.  
  220. itods1:
  221.    sub al,cl
  222.    inc ah
  223.    jnb itods1
  224.  
  225.    add al,cl
  226.    dec ah
  227.  
  228.    add ax,'00'
  229.    mov ds:[di],ah
  230.    inc di
  231.    mov ds:[di],al
  232.    pop cx
  233.    ret
  234.  
  235.    assume ds:nothing
  236. itods endp
  237.  
  238.  
  239. page
  240. ;-----------------------------------------
  241. ; null terminated string copy
  242. ;
  243. ; entry:  ds:si - source
  244. ;         cs:di - destination
  245. ;
  246. strcpy proc near
  247.    mov al,ds:[si]
  248.    mov cs:[di],al
  249.    inc si
  250.    inc di
  251.    cmp al,0
  252.    jnz strcpy
  253.    ret
  254. strcpy endp
  255.  
  256.  
  257. ;-----------------------------------------
  258. ; see if a file can be accessed
  259. ;
  260. ; entry:   ds:dx     - filename
  261. ; exit:    cy        - set if file is missing
  262. ;
  263. access_file proc near
  264.    mov ax,3d00h               ;open file in read mode
  265.    int 21h
  266.    jb cant_open
  267.  
  268.    mov bx,ax
  269.    mov ax,3e00h               ;close file
  270.    int 21h
  271.  
  272. cant_open:
  273.    ret
  274. access_file endp
  275.  
  276.  
  277. page
  278. subttl Interrupt Service Section
  279. ;=========================================
  280.  
  281.    assume ds:nothing
  282.  
  283. ;-----------------------------------------
  284. ; catch int20 vector - program terminate
  285. ;
  286. catch_20:
  287.    call log_activity
  288.    jmp old_int_20
  289.  
  290.  
  291. ;-----------------------------------------
  292. ; catch the int21 vector - dos services
  293. ;
  294. catch_21:
  295.  
  296. ;check for functions that control logging
  297.    cmp ah,4bh
  298.    jz handle_exec
  299.    cmp ah,4ch
  300.    jz handle_terminate
  301.    cmp ah,0
  302.    jz handle_terminate
  303.  
  304. ;check functions that are counted
  305.    cmp ah,3fh
  306.    jz handle_read
  307.    cmp ah,40h
  308.    jz handle_write
  309.  
  310. use_old_21_other:
  311.    inc exec_others
  312.  
  313. use_old_21:
  314.    jmp old_int_21
  315.  
  316.  
  317. ;-----------------------------------------
  318. ; handle read requests
  319. ;
  320. handle_read:
  321.    inc exec_reads
  322.    jmp use_old_21
  323.  
  324.  
  325. ;-----------------------------------------
  326. ; handle write requests
  327. ;
  328. handle_write:
  329.    inc exec_writes
  330.    jmp use_old_21
  331.  
  332. page
  333. ;-----------------------------------------
  334. ; process terminate calls
  335. ;
  336. handle_terminate:
  337.    call log_activity
  338.    jmp use_old_21
  339.  
  340.  
  341. ;-----------------------------------------
  342. ; process exec calls
  343. ;
  344. handle_exec:
  345.    cmp al,0                    ;make sure this is not an overlay exec
  346.    jnz use_old_21_other        ;don't log overlays
  347.  
  348.    pushall
  349.  
  350.    call access_file            ;see if the exec is going to be possible
  351.    jc bad_exec                 ;don't bother logging if not.  many programs
  352.                                ;search PATH= with multiple bad execs.
  353.  
  354.    call log_activity           ;log current process in case of process
  355.                                ;nesting.  this should be improved to
  356.                                ;allow a stack of nested processes.
  357.  
  358.    mov si,dx
  359.    mov di,offset exec_name     ;save filename
  360.    call strcpy
  361.  
  362.    push cs
  363.    pop ds
  364.    assume ds:cseg
  365.  
  366.    mov ah,2ah
  367.    int 21h                     ;get start date from dos
  368.    sub cx,1900
  369.    itod log_year,cl
  370.    itod log_month,dh
  371.    itod log_day,dl
  372.  
  373.    mov ah,2ch
  374.    int 21h                     ;get start time from dos
  375.    mov exec_hour,ch
  376.    mov exec_min,cl
  377.    mov exec_sec,dh
  378.    mov exec_hund,dl
  379.    itod log_hour,ch
  380.    itod log_min,cl
  381.    itod log_sec,dh
  382.  
  383.    mov exec_reads,0            ;clear the resource accumulators
  384.    mov exec_writes,0
  385.    mov exec_others,0
  386.  
  387. bad_exec:
  388.    popall
  389.    assume ds:nothing
  390.  
  391.    jmp use_old_21
  392.  
  393. page
  394. ;-----------------------------------------
  395. ; place the ident into the log record
  396. ;
  397. place_ident proc near
  398.    assume ds:cseg
  399.  
  400.    mov si,offset exec_name   ;where to get next char from
  401.  
  402. place_again:
  403.    mov cx,12                 ;count of chars to be placed
  404.    mov di,offset log_ident   ;where to place the next char
  405.  
  406. place_next:
  407.    mov al,ds:[si]
  408.    inc si
  409.  
  410.    cmp al,0
  411.    jz place_filler      ;check for terminators
  412.    cmp cx,0             ;or overlong filenames
  413.    jz place_filler
  414.  
  415.    cmp al,'\'
  416.    jz place_again
  417.    cmp al,'/'           ;check for separators - reset to start of name
  418.    jz place_again       ;on any of these
  419.    cmp al,':'
  420.    jz place_again
  421.  
  422.    cmp al,'Z'
  423.    ja place_char
  424.    cmp al,'A'           ;is the character upper case?
  425.    jb place_char
  426.  
  427.    add al,' '           ;map it to lower case if needed
  428.  
  429. place_char:
  430.    mov ds:[di],al       ;place the character
  431.    inc di
  432.    dec cx
  433.    jmp place_next
  434.  
  435. ;fill rest of filename field with blanks
  436. place_filler:
  437.    cmp cx,0
  438.    jz end_ident
  439.  
  440.    mov byte ptr ds:[di],' '
  441.    inc di
  442.    dec cx
  443.    jmp place_filler
  444.  
  445. end_ident:
  446.    ret
  447. place_ident endp
  448.  
  449.  
  450. page
  451. ;-----------------------------------------
  452. ; place the programs running time into the log
  453. ;
  454. place_runtime proc near
  455.       mov ah,2ch
  456.       int 21h               ;get termination time from dos
  457.  
  458.       mov al,dl  ;stop_hund
  459.       sub al,exec_hund
  460.       pushf
  461.       jnc r0
  462.  
  463.       add al,100           ;borrow 1 second  (100/100)
  464. r0:   itod log_runhund,al
  465.       mov al,dh  ;stop_sec
  466.       popf
  467.       sbb al,exec_sec
  468.       pushf
  469.       jnc r1
  470.  
  471.       add al,60            ;borrow 1 minute  (60 seconds)
  472. r1:   itod log_runsec,al
  473.       mov al,cl  ;stop_min
  474.       popf
  475.       sbb al,exec_min
  476.       pushf
  477.       jnc r2
  478.  
  479.       add al,60            ;borrow 1 hour  (60 mins)
  480. r2:   itod log_runmin,al
  481.       mov al,ch  ;stop_hour
  482.       popf
  483.       sbb al,exec_hour
  484.       jnc r3
  485.  
  486.       add al,24           ;borrow 1 day  (24 hours)
  487. r3:   itod log_runhour,al
  488.       ret
  489. place_runtime endp
  490.  
  491.  
  492.  
  493. ;-----------------------------------------
  494. ; place the programs resource usage counts into the log
  495. ;
  496. place_counts proc near
  497.       wtoh log_reads,exec_reads
  498.       wtoh log_writes,exec_writes
  499.       wtoh log_others,exec_others
  500.       ret
  501. place_counts endp
  502.  
  503. page
  504. ;-----------------------------------------
  505. ; write the current log entry to the logfile
  506. ;
  507. write_log_entry proc near
  508.    mov ax,3d02h               ;open file in update mode
  509.    mov dx,offset log_filename
  510.    int 21h
  511.    jnb log_open
  512.  
  513. ;couldn't open the logfile, try to create it instead
  514.    mov ax,3c00h               ;create file
  515.    mov cx,0                   ;attributes
  516.    mov dx,offset log_filename
  517.    int 21h
  518.  
  519. log_open:
  520.    mov bx,ax
  521.    mov ax,4202h               ;seek to end of file
  522.    mov cx,0
  523.    mov dx,cx
  524.    int 21h
  525.  
  526.    mov ax,4000h               ;write to file
  527.    mov dx,offset log_record   ;what to write
  528.    mov cx,log_length          ;how much to write
  529.    int 21h                    ;perform the write
  530.  
  531.    mov ax,3e00h               ;close file
  532.    int 21h
  533.    ret
  534. write_log_entry endp
  535.  
  536.  
  537. ;-----------------------------------------
  538. ; make an activity log entry
  539. ;
  540. log_activity proc near
  541.    pushall
  542.    push cs
  543.    pop ds
  544.  
  545. ;see if there is anything to log
  546.    cmp byte ptr exec_name,0
  547.    jz logex
  548.  
  549. ;place filename
  550.    call place_ident
  551.  
  552. ;place running time
  553.    call place_runtime
  554.  
  555. ;place the resource counts
  556.    call place_counts
  557.  
  558. ;write the completed log entry to the logfile
  559.    call write_log_entry
  560.  
  561.    mov byte ptr exec_name,0      ;remove filename from memory
  562.  
  563. logex:
  564.    popall
  565.    ret
  566.  
  567.    assume ds:nothing
  568. log_activity endp
  569.  
  570.  
  571. page
  572. subttl Program Startup and Initialization Section
  573. ;=========================================
  574.  
  575. ;-----------------------------------------
  576. ; all of the following code is over-written after startup
  577. ;
  578. last_resident db 0
  579.  
  580. ;
  581. ; startup entry point
  582. ;
  583. startup:
  584.    push cs
  585.    pop ds
  586.    assume ds:cseg
  587.  
  588. ;
  589. ; install the dos-services vector
  590. ;
  591.    mov ax,3521h
  592.    int 21h                       ;get old int21h vector
  593.    mov word ptr old_int_21,bx
  594.    mov word ptr old_int_21[2],es
  595.  
  596.    mov ax,es:signature
  597.    cmp ax,cs:signature
  598.    jnz new_installation           ;check for re-installation
  599.    jmp already_resident
  600.  
  601. new_installation:
  602.    mov ax,2521h
  603.    mov dx,offset catch_21        ;set new 21h vector
  604.    int 21h
  605.  
  606. ;
  607. ; install the terminate process vector
  608. ;
  609.    mov ax,3520h
  610.    int 21h                       ;get old int20h vector
  611.    mov word ptr old_int_20,bx
  612.    mov word ptr old_int_20[2],es
  613.    mov ax,2520h
  614.    mov dx,offset catch_20        ;set new 20h vector
  615.    int 21h
  616.  
  617. ;
  618. ; terminate and stay resident
  619. ;
  620. go_resident:
  621.    mov ah,9
  622.    mov dx,offset signon_str
  623.    int 21h
  624.  
  625.    mov dx,offset last_resident
  626.    mov cl,4
  627.    shr dx,cl
  628.    inc dx
  629.    mov ah,31h
  630.    int 21h
  631.  
  632. signon_str:
  633.    db 13,10,'System Activity Logger v1.1 (2/2/87 SHS)',13,10,'$'
  634.    db 13,10,'(C) 1987 Samuel H. Smith, all commercial rights reserved.',13,10,26
  635.  
  636. ;
  637. ; looks like we are already resident
  638. ;
  639. already_resident:
  640.    mov ah,9
  641.    mov dx,offset errormsg_str
  642.    int 21h
  643.  
  644.    mov ah,0
  645.    int 21h
  646.  
  647. errormsg_str:
  648.    db 13,10,'SysAct is already resident.',13,10,'$'
  649.  
  650. cseg ends
  651. end entry
  652.  
  653.  
  654.